package day4;

//克隆对象：
//浅克隆：需要实现一个Cloneable接口，表示支持克隆，重写
//父类的Clone()
//深克隆：需要实现一个Cloneable接口，表示支持克隆，重写
//父类的Clone():对象属性也要这么做
public class Emp implements Cloneable {
    Integer id;
    String name;
    EmpInfo info;

    @Override
    protected Emp clone() throws CloneNotSupportedException {
        Emp e = (Emp) super.clone();
        e.info =info.clone();
        return e;
    }

    public Emp() {
        System.out.println("构造方法");
    }
}

class EmpInfo implements Cloneable{
    Integer id;
    String sex;
    Integer age;

    @Override
    protected EmpInfo clone() throws CloneNotSupportedException {
        return (EmpInfo) super.clone();
    }
}

class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Emp e = new Emp();//走构造
        e.id = 100;
        e.name = "邓志涛";
        e.info = new EmpInfo();
        e.info.id = 100;
        e.info.age = 60;
        e.info.sex = "男";
        Emp e2 = e.clone();//在堆内存直接复制一个对象出来
        System.out.println(e + " " + e.info);
        System.out.println(e2 + " " + e2.info);
        e.name = "唐宇轩";
        e.info.age = 18;
        System.out.println(e2.name+" "+e2.info.age);
    }
}
